package com.cskaoyan.market.util;

import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;

import javax.validation.Validator;

/**
 * @author lixianbiao
 * @since 2024/06/29 09:22
 */
public class ValidUtil {
    public static void valid(Validator validator, Object target, Object... group){
        BindingResult bindingResult = new BeanPropertyBindingResult(target.getClass(),target.getClass().getName());
        SpringValidatorAdapter adapter = new SpringValidatorAdapter(validator);
        adapter.validate(target,bindingResult);
        if(bindingResult.hasErrors()){
            try {
                throw new BindException(bindingResult);
            } catch (BindException e) {
                throw new RuntimeException(e);
            }
        }
    }
}